package com.xiangmo.travel.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangmo.travel.Dto.CommunityLikeDto;
import com.xiangmo.travel.Dto.LikeDto;
import com.xiangmo.travel.Entity.CommunityLike;
import com.xiangmo.travel.Entity.Like;
import com.xiangmo.travel.Service.CommunityLikeService;
import com.xiangmo.travel.mapper.CommunityLikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommunityLikeServiceImpl implements CommunityLikeService {
    @Autowired
    private CommunityLikeMapper communityLikeMapper;
    @Override
    public int addCommunityLike(CommunityLikeDto communityLikeDto) {
        CommunityLike communitylike = CommunityLike.builder()
                .communityId(communityLikeDto.getCommunityId())
                .userId(communityLikeDto.getUserId())
                .build();
        return communityLikeMapper.insert(communitylike);
    }

    @Override
    public int deleteCommunityLike(CommunityLikeDto communityLikeDto) {
        LambdaQueryWrapper<CommunityLike> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CommunityLike::getCommunityId, communityLikeDto.getCommunityId())
                .and(i -> i.eq(CommunityLike::getUserId, communityLikeDto.getUserId()));
        return communityLikeMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public int checkCommunityIsLike(CommunityLikeDto communityLikeDto) {
        LambdaQueryWrapper<CommunityLike> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CommunityLike::getUserId, communityLikeDto.getUserId())
                .and(i -> i.eq(CommunityLike::getCommunityId, communityLikeDto.getCommunityId()));
        CommunityLike communityLike = communityLikeMapper.selectOne(lambdaQueryWrapper);
        return communityLike == null ? 0 :1;
    }
}
